<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>添加通知</title>
  <link rel="stylesheet" href="/static/css/layui.css">
</head>
<body>
  <div class="layui-card">
    <div class="layui-card-header">添加通知</div>
    <div class="layui-card-body">
      <form class="layui-form" action="#" method="post" lay-filter="addNoticeForm">
        <!-- 通知内容 -->
        <div class="layui-form-item">
          <label class="layui-form-label">通知内容</label>
          <div class="layui-input-block">
            <textarea name="notice_content" required lay-verify="required" 
                      placeholder="请输入通知内容" autocomplete="off" class="layui-textarea"></textarea>
          </div>
        </div>

        <!-- 提交和重置按钮 -->
        <div class="layui-form-item">
          <div class="layui-input-block">
            <button type="button" class="layui-btn" lay-submit lay-filter="submitNotice">提交</button>
            <button type="reset" class="layui-btn layui-btn-primary">重置</button>
          </div>
        </div>
      </form>
    </div>
  </div>

  <script>
  layui.use(['form', 'layer', 'jquery'], function() {
    var form = layui.form;
    var layer = layui.layer;
    var $ = layui.jquery;

    // 监听提交事件
    form.on('submit(submitNotice)', function(data) {
      const noticeContent = data.field.notice_content;

      // 向后端发送数据
      $.ajax({
        url: '/add_notice',  // 后端添加通知的 API
        method: 'POST',
        contentType: 'application/json',
        data: JSON.stringify({
          notice_content: noticeContent
        }),
        success: function(res) {
          if (res.success) {
            layer.msg('通知添加成功！', {icon: 1});
          } else {
            layer.msg('通知添加失败，请重试！', {icon: 2});
          }
        },
        error: function() {
          layer.msg('请求失败，请检查网络！', {icon: 2});
        }
      });

      return false;  // 阻止表单的默认提交
    });
  });
  </script>
</body>
</html>
